package view 
{
	import cn.inswf.framework.core.IRegister;
	import data.Chapter;
	import data.Lesson;
	import data.LMS;
	import event.ActionEvent;
	import event.AiccEvent;
	import event.LessonEvent;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import util.ShareEvent;
	
	/**
	 * ...
	 * @author liwei
	 */
	public class MediaPlayerView extends Sprite implements IRegister
	{
		[Inject]public var controller:SkinView;
		[Inject]public var menu:MenuView;
		[Inject]public var help:HelpView;
		private var _movie:MovieClip;
		private var _loader:Loader;
		private var _mask:Shape;
		private var _con:Sprite;
		private var _lesson:Lesson;
		private var _soundtrans:SoundTransform;
		private var _lastFrame:int = -1;
		public function MediaPlayerView() 
		{
			super();
			_soundtrans = new SoundTransform();
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
			_loader.contentLoaderInfo.addEventListener(Event.INIT, loader_init);
			_loader.contentLoaderInfo.sharedEvents.addEventListener(DataEvent.DATA, sharedhandler);
			_con = new Sprite();
			addChild(_con);
			_con.addChild(_loader);
			_mask = new Shape();
			addChild(_mask);
			_mask.graphics.beginFill(0x00ff00, 0.5);
			_mask.graphics.drawRect(Config.rect.x, Config.rect.y, Config.rect.width, Config.rect.height);
			_mask.graphics.endFill();
			_con.x = Config.rect.x;
			_con.y = Config.rect.y;
			_con.mask = _mask;
		}
		
		private function sharedhandler(e:DataEvent):void 
		{
			ShareEvent.dispatchEvent(new ActionEvent(ActionEvent.ACTION, e.data));
		}
		public function loadLesson(id:int):void {
			var item:Lesson = Chapter.getLessonById(id);
			setLesson(item);
		}
		public function load(chapterId:int, lesson:int):void {
			var item:Lesson = Chapter.getLessonByFlag(chapterId, lesson);
			setLesson(item);
		}
		private function setLesson(lesson:Lesson):void {
			removeEventListener(Event.ENTER_FRAME, enterframe);
			_movie = null;
			if (lesson == null) return;
			_loader.unloadAndStop();
			_lastFrame = -1;
			_loader.load(new URLRequest(lesson.path+"?mastery_score="+LMS.mastery_score));
			_lesson = lesson;
			_lesson.start();
			State.control = _lesson.control != -1;
			State.chapter = _lesson.chapter;
			State.lesson = _lesson.lessonId;
			ShareEvent.dispatchEvent(new AiccEvent(AiccEvent.SAVE));
			controller.startLesson();
		}
		private function loader_init(e:Event):void 
		{
			try {
				var info:LoaderInfo = e.target as LoaderInfo;
				if (info != null) {
					State.fps = info.frameRate;
					stage.frameRate = State.fps;
				}
				_movie = _loader.content as MovieClip;
				_movie.soundTransform = _soundtrans;
				if (_movie != null) {
					if (_movie.totalFrames > 1) {
						State.oneFrame = false;
					}else {
						State.oneFrame = true;
					}
					if (!_lesson.notMovie) {
						if (State.showMenu) {
							stop();
						}else {
							if (State.pause) {
								stop();
							}else {
								play();
							}
						}
					}
				}else {
					State.oneFrame = true;
				}
			}catch (e:Error) {
				
			}
			addEventListener(Event.ENTER_FRAME, enterframe);
		}
		
		private function loader_complete(e:Event):void 
		{
			
		}
		public function setVolume(value:Number):void {
			_soundtrans.volume = value;
			if (_movie != null) {
				_movie.soundTransform = _soundtrans;
			}
		}
		public function showMenu():void {
			stop();
			State.showMenu = true;
			if (menu.parent == null) {
				addChild(menu);
			}
		}
		public function hideMenu():void {
			play();
			State.showMenu = false;
			if (menu.parent != null) {
				menu.parent.removeChild(menu);
			}
		}
		public function play():void {
			if (State.oneFrame) return;
			if (!_lesson.notMovie) {
				State.pause = false;
				if (_movie!=null) {
					_movie.play();
				}
			}
			//state.state = false;
		}
		public function stop():void {
			if (State.oneFrame) return;
			if (!_lesson.notMovie) {
				State.pause = true;
				if (_movie != null) {
					_movie.stop();
				}
			}
			//state.state = true;
		}
		public function finishPostition():void {
			if (State.pause) return;
			_movie.play();
		}
		public function setPosition(p:Number):void {
			if (State.oneFrame) return;
			var tf:int = Math.round(p * _movie.totalFrames);
			if (tf >= _movie.totalFrames) {
				tf = _movie.totalFrames - 1;
			}
			_movie.gotoAndStop(tf);
		}
		
		/* INTERFACE cn.inswf.framework.core.IRegister */
		
		public function onRegister():void 
		{
			addChild(controller);
			addChild(help);
			help.visible = false;
		}
		
		public function onInject(name:String, instance:Object):void 
		{
			
		}
		private function enterframe(e:Event):void {
			if (_movie != null) {
				var cf:int = _movie.currentFrame;
				var tf:int = _movie.totalFrames;
				State.currentFrame = cf;
				State.totalFrame = tf;
				if (cf >= tf) {
					if (!State.positioning) {
						if (!State.pause) {
							if (!_lesson.notMovie) {
								_lesson.complete();
							}
							ShareEvent.dispatchEvent(new AiccEvent(AiccEvent.SAVE));
							dispatchEvent(new LessonEvent(LessonEvent.NEXT));
						}
					}
				}else {
					if (!State.positioning) {
						if (cf == _lastFrame) {
							trace("stop");
							stop();
						}
					}
					_lastFrame = cf;
				}
			}else {
				State.currentFrame = 1;
				State.totalFrame = 1;
			}
			controller.update();
		}
		
		public function get loader():Loader 
		{
			return _loader;
		}
	}

}